const { Router } = require('express')
const express = require('express')
//路由容器
const address = express.Router()
const axios = require('axios')
const { JsonWebTokenError } = require('jsonwebtoken')
const jwt = require("jsonwebtoken")
address.post('/address/addAddress', (req, res) => {
    let {address,addressee,addresseeTele,setting}=req.body.params
   axios({
       method:'post',
       url : '/address/addAddress',
       headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token:req.session.token
    },
       params:{
        address:address,
        addressee:addressee,
        addresseeTele:addresseeTele,
        setting:setting,
        userId:jwt.decode(req.session.token).id
    }
   }).then(function(data){
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
       res.status(500).json({
           msg:err
       })
   })
})
address.get('/address/queryAddress', (req, res) => {
   axios({
        method:'get',
        url : '/address/queryAddress',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        params:{
            userId:jwt.decode(req.session.token).id
        }
   }).then(function(data){
       console.log("------------------------------------------------------------")
       console.log(data)
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
    console.log("------------------------------------------------------------")
       console.log(err)
       console.log("------------------------------------------------------------")
       res.status(500).json({
           msg:err
       })
   })
})
address.get('/address/deleteAddress', (req, res) => {
    let {addressId}=req.query
   axios({
        method:'delete',
        url : '/address/deleteAddress',
        params:{
            addressId:addressId
        }
   }).then(function(data){
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
       res.status(500).json({
           msg:err
       })
   })
})
address.put('/address/updateAddress', (req, res) => {
    console.log(req.query)
    let {addressId,address,addressee,addresseeTele,setting}=req.body.params
   axios({
        method:'put',
        url : '/address/updateAddress',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        params:{
            addressId:addressId,
            address:address,
            addressee:addressee,
            addresseeTele:addresseeTele,
            setting:setting,
            userId:jwt.decode(req.session.token).id
        }
   }).then(function(data){
       res.status(200).json({
           data:data.data,
           code:0
       })
   }).catch(function(err){
       console.log(err)
       res.status(500).json({
           msg:err
       })
   })
})

module.exports=address;